﻿

using Furion;
using IotClientWeb.Options;
using ProtoBuf.Meta;
using XT.Common.Config;
using XT.Common.Services;
using XT.Common.Extensions;
using XT.WinService;
using IotClientService;
using IGeekFan.AspNetCore.Knife4jUI;
using Furion.SpecificationDocument;
using Microsoft.AspNetCore.HttpOverrides;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using IotClientWeb.Unity;

using IotClientWeb.Services;

namespace IotClientWeb;

public class Startup:AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        ShareService.ConfigureLog4net();

        services.AddSingleton(new AppSettings(false));

        //services.AddSingleton<IRealDbService, InfluxDbService>();

        // services.AddSingleton<IFileService, FileService>();

        // 允许跨域
        services.AddCorsAccessor();

        // 任务队列
        services.AddTaskQueue();

       
        // 添加配置项
        services.AddProjectOptions();

        // 注入grpc服务
        services.RegisterClientGrpc();
        services.RegisterClientService(true);


        // window服务启动服务
        ShareService.AddWinService(services);
        services.AddSingleton<IWindowService, WebWinService>();

      

        services.AddControllers().AddInjectWithUnifyResult<AdminResultProvider>();







        // 配置Nginx转发获取客户端真实IP
        // 注1：如果负载均衡不是在本机通过 Loopback 地址转发请求的，一定要加上options.KnownNetworks.Clear()和options.KnownProxies.Clear()
        // 注2：如果设置环境变量 ASPNETCORE_FORWARDEDHEADERS_ENABLED 为 True，则不需要下面的配置代码
        services.Configure<ForwardedHeadersOptions>(options =>
        {
            options.ForwardedHeaders = ForwardedHeaders.All;
            options.KnownNetworks.Clear();
            options.KnownProxies.Clear();
        });


    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseInject(string.Empty);

        // 配置Swagger-Knife4UI（路由前缀一致代表独立，不同则代表共存）
        app.UseKnife4UI(options =>
        {
            options.RoutePrefix = "kapi";
            foreach (var groupInfo in SpecificationDocumentBuilder.GetOpenApiGroups())
            {
                options.SwaggerEndpoint("/" + groupInfo.RouteTemplate, groupInfo.Title);
            }
        });



        app.UseRouting();

        app.UseCorsAccessor();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseInject();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }
}
